package fi.helsinki.cs.kaisei;

import java.util.ArrayList;

/**
 * @author Team TA's
 */

public class Report {

        // visible to subclasses
        protected ArrayList<Timetable> timetables;
        
        public Report(ArrayList<Timetable> timetables) {
                this.timetables = timetables;
        }
        
        public ArrayList<Timetable> getTimetables() {
            return timetables;
        }
        
        public String toString() {   
          
          String res = "";
                
          for (Timetable timetable : timetables) {
                String [][] data =  new String [3][7];
                for (int i = 0 ; i < 3; i++) {
                                for (int j = 0; j < 7 ; j++){
                                        data[i][j] = "|\t";
                                }
                        }
                boolean tabPlaced = false;
                res += "\n\n\tFrom week " + timetable.getStartWeek() + " to " + timetable.getEndWeek() + "\n";
                res += "_________________________________________________________________________________________________________________________________\n|\t\t";
                for (int i = 1; i <= 7 ; i++){
                        res += "|" + Weekday.longNameMap.get(Weekday.intToEnumMap.get(i)) + "   \t";
                }
                res += "|\n";
                res += "|_______________|_______________|_______________|_______________|_______________|_______________|_______________|_______________|\n";
           
                String startTime = "";
                String endTime = "";
                int hourCounter = 0;
                for (int aika = 1; aika <= 6 ; aika++){
                        if (hourCounter == 0) {
                                res += "|08:00 - 10:00\t";
                                startTime = "08";
                                endTime = "10";
                        }
                        else if (hourCounter == 1){
                                res += "|10:00 - 12:00\t";
                                startTime = "10";
                                endTime = "12";
                        }
                        else if (hourCounter == 2){
                                res += "|12:00 - 14:00\t";
                                startTime = "12";
                                endTime = "14";
                        }
                        else if (hourCounter == 3){
                                res += "|14:00 - 16:00\t";
                                startTime = "14";
                                endTime = "16";
                        }
                        else if (hourCounter == 4){
                                res += "|16:00 - 18:00\t";
                                startTime = "16";
                                endTime = "18";
                        }
                        else if (hourCounter == 5){
                                res += "|18:00 - 20:00\t";
                                startTime = "18";
                                endTime = "20";
                        }
                        
                        for (int pvm = 1; pvm <= 7; pvm++) {
                                tabPlaced = false;
                                for (Weekday.Day day : timetable.getSchedule().keySet()){
                                        if (day.toString().equalsIgnoreCase(Weekday.intToEnumMap.get(pvm).toString())) {
                                                tabPlaced = true;
                                                for (Event event : timetable.getSchedule().get(day)){
                                                        
                               
                               if (
                                        (Integer.valueOf(event.getStartTime()).intValue() == Integer.valueOf(startTime).intValue()) || 
                                        (Integer.valueOf(event.getStartTime()).intValue() <= Integer.valueOf(startTime).intValue() && 
                                        Integer.valueOf(event.getEndTime()).intValue() >= Integer.valueOf(endTime).intValue())
                               ) {
                               
                                               
                                                                tabPlaced = true;
                                                                if (event.getTitle().length() <= 6)
                                                                        data[0][pvm-1] = "|" + event.getTitle() + "\t";
                                                                else if (event.getTitle().length() >= 14) {
                                                                        data[0][pvm-1] = "|" + event.getTitle().substring(0, 14);
                                                                }
                                                                else
                                                                        data[0][pvm-1] = "|" + event.getTitle() + "";
                                                                
                                                                String time = "|" + event.getStartTime() + ":00 - " + event.getEndTime()+":00";
                                                                data[1][pvm-1] = time;
                                                                
                                                                if (event.getLocation().length() <= 7)
                                                                        data[2][pvm-1] = "|" + event.getLocation() + "\t";
                                                                else if (event.getLocation().length() >= 14) {
                                                                        data[2][pvm-1] = "|" + event.getLocation().substring(0, 14);
                                                                }
                                                                else
                                                                        data[2][pvm-1] = "|" + event.getLocation() + "";
                                                        
                                                        }
                                                        else {
                                                                if (tabPlaced == false) {
                                                                        tabPlaced = true;
                                                                }
                                                        }
                                                }
                                        }
                                        else {
                                                if (tabPlaced == false) {
                                                                tabPlaced = true;
                                                        }
                                        }
                                }
                        }
                                        
                        res +=                  ""+data[0][0]+"\t"+data[0][1]+"\t"+data[0][2]+"\t"+data[0][3]+"\t"+data[0][4]+"\t"+data[0][5]+"\t"+data[0][6]+"\t|";
                        res += "\n|               "+data[1][0]+"\t"+data[1][1]+"\t"+data[1][2]+"\t"+data[1][3]+"\t"+data[1][4]+"\t"+data[1][5]+"\t"+data[1][6]+"\t|";
                        res += "\n|               "+data[2][0]+"\t"+data[2][1]+"\t"+data[2][2]+"\t"+data[2][3]+"\t"+data[2][4]+"\t"+data[2][5]+"\t"+data[2][6]+"\t|";
                        res += "\n|_______________|_______________|_______________|_______________|_______________|_______________|_______________|_______________|\n";
                        hourCounter++;
                
                        for (int i = 0; i < 3; i++) {
                                for (int j = 0; j < 7; j++){
                                        data[i][j] = "|\t";
                                }
                        }
                }
                
        }
        return res;
    }
    
}
